import router from '@system.router';
import usbManager from '@ohos.usbManager';
import CheckEmptyUtils from '../../utils/CheckEmptyUtils.js';

export default {
    data: {
        result: 'World',
        resultMode: "",
        usbPort: null,
        ports: null,
        resultStatus: "",
        supportMode:0,
        versionName: "",
        resultModeInfo: ""
    },
    onInit() {
        this.versionName = usbManager.getVersion();
        this.getFirstPort();
    },
    getFirstPort() {
        try {
            this.ports = usbManager.getPorts();
            if (CheckEmptyUtils.isEmptyArr(this.ports)) {
                this.result = "init error: 查询port结果为空"
                console.info("usb port init error: port is null")
                return;
            }
            this.result = "usb port list： " + JSON.stringify(this.ports);
            this.usbPort = this.ports[0];
            console.info("usb port : " + JSON.stringify(this.usbPort));

            // 17之后的版本 host的currentMode = 2 device的currentMode = 1
            var iVer = parseInt(this.versionName.slice(-3), 10)
            var DevMode = 0
            var HostMode = 0
            if (iVer > 17) {
                DevMode = 1
                HostMode = 2
            } else {
                DevMode = 2

                HostMode = 1
            }
            var currentMode = this.usbPort.status.currentMode;
            if (currentMode == HostMode) {
                this.resultMode = "host";
            } else if (currentMode == DevMode) {
                this.resultMode = "device";
            }
        } catch (err) {
            console.info('usb getPorts catch err code: ' + err.code + ' message: ' + err.message);
            this.resultModeInfo = 'usb getPorts catch err code: ' + err.code + ' message: ' + err.message;
        }
    },
    onBackPress() {
        router.back();
    },
    getSupportMode() {
        if (CheckEmptyUtils.isEmpty(this.usbPort)) {
            this.supportMode = "port为空，无法查询"
            return;
        }
        try {
            this.supportMode = usbManager.getSupportedModes(this.usbPort.id);
        } catch (err) {
            console.info('usb getSupportedModes catch err code: ' + err.code + ' message: ' + err.message);
            this.result = 'usb getSupportedModes catch err code: ' + err.code + ' message: ' + err.message;
        }
    },

/**
     * POWER_ROLE_NONE --- 0
     * POWER_ROLE_SOURCE --- 1
     * POWER_ROLE_SINK --- 2
     */

/**
     * DATA_ROLE_NONE,
     * DATA_ROLE_HOST,
     * DATA_ROLE_DEVICE
     */

    setHostMode(isHost) {
        if (CheckEmptyUtils.isEmpty(this.usbPort)) {
            console.info("usb error, set power role:  this.usbPort is null");
            return;
        }
        if (isHost) {
            if (this.usbPort.status.currentPowerRole == 1 && this.usbPort.status.currentDataRole == 1) {
                console.info("usb set role, current role is already host");
                this.resultStatus = "usb port role is already host ";
                return;
            }
            usbManager.setPortRoles(this.usbPort.id, 1, 1).then(retStatus=>{
                console.info("usb set role, retStatus " + retStatus);
                this.resultStatus = "usb set role, retStatus :" + retStatus;
                //刷新列表查看结果是否设置成功
                this.getFirstPort();
            }).catch(error=>{
                console.info("usb set role, error " + error);
                this.resultStatus = "usb set role, error :" + error;
            });
        } else {
            if (this.usbPort.status.currentPowerRole == 2 && this.usbPort.status.currentDataRole == 2) {
                console.info("usb set role, current role is already device");
                this.resultStatus = "usb port role is already device";
                return;
            }
            usbManager.setPortRoles(this.usbPort.id, 2, 2).then(retStatus=>{
                console.info("usb set role, retStatus " + retStatus);
                this.resultStatus = "usb set role, retStatus :" + retStatus;
                //刷新列表查看结果是否设置成功
                this.getFirstPort();
            }).catch(error=>{
                console.info("usb set role, error " + error);
                this.resultStatus = "usb set role, error :" + error;
            });;
        }
    },

    setPowerRole(powerRole) {
        console.info("usb set power role: " + powerRole);

        if (CheckEmptyUtils.isEmpty(this.usbPort)) {
            console.info("usb error, set power role:  this.usbPort is null");
            return;
        }

        var currentPowerRole = this.usbPort.status.currentPowerRole;
        console.info("usb port currentPowerRole: " + currentPowerRole);
        if (currentPowerRole == powerRole) {
            console.info("usb set power role, current power role is the same: " + powerRole);
            return;
        }

        if (powerRole == 1) {
            console.info("usb port set powerRole to NAPI: " + currentPowerRole);
            this.resultStatus = usbManager.setPortRoles(this.usbPort.id, 1, this.usbPort.status.currentDataRole);
        } else if (powerRole == 2) {
            console.info("usb port set powerRole to NAPI: " + currentPowerRole);
            this.resultStatus = usbManager.setPortRoles(this.usbPort.id, 2, this.usbPort.status.currentDataRole);
        } else {
            console.info("usb set power role invalid role : " + powerRole);
        }
        console.info("usb set power role resultStatus : " + this.resultStatus);

        //刷新列表查看结果是否设置成功
        this.getFirstPort();
    },
    setDataRole(dataRole) {
        console.info("usb set data role: " + dataRole);

        if (CheckEmptyUtils.isEmpty(this.usbPort)) {
            console.info("usb error, set data role:  this.usbPort is null");
            return;
        }

        var currentDataRole = this.usbPort.status.currentDataRole;
        if (currentDataRole == dataRole) {
            console.info("usb set data role, current data role is the same: " + dataRole);
            return;
        }

        if (dataRole == 1) {
            console.info("usb port set powerRole to NAPI: " + currentDataRole);
            this.resultStatus = usbManager.setPortRoles(this.usbPort.id, this.usbPort.status.currentPowerRole, 1);
        } else if (dataRole == 2) {
            console.info("usb port set powerRole to NAPI: " + currentDataRole);
            this.resultStatus = usbManager.setPortRoles(this.usbPort.id, this.usbPort.status.currentPowerRole, 2);
        } else if (dataRole == 0){
            console.info("usb port set powerRole to NAPI: " + currentDataRole);
            this.resultStatus = usbManager.setPortRoles(this.usbPort.id, this.usbPort.status.currentPowerRole, 2);
        } else {
            console.info("usb set data role invalid role : " + dataRole);
        }
        console.info("usb set power role resultStatus : " + this.resultStatus);

        //刷新列表查看结果是否设置成功
        this.getFirstPort();
    }
}
